package com.otherTest.java8.tomap;


import lombok.Data;
import org.junit.Test;

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: conceit
 * @Date: 2018/7/20 0020
 * @see com.otherTest.java8.tomap.TestToMap
 */
@SuppressWarnings("all")
public class TestToMapThree {

    /**
     * @Description: 第三个方法
     * 按照性别取出收入最高的人
     * @param:
     * @return:
     * @author: conceit
     * @Date: 2018/7/20 0020
     */
    @Test
    public void doTestExampleOne() {

        Map<Employee.Gender, Employee> highestEarnerByGender = TestToMapThree.persons().stream().collect(
                Collectors.toMap(Employee::getGender, Function.identity(), (oldPerson, newPerson) ->
                        newPerson.getIncome() > oldPerson.getIncome() ? newPerson : oldPerson)
        );
        System.out.println(highestEarnerByGender);

    }

    /**
     * @Description: 第三个方法
     * 按照性别取出id最高的人
     * @param:
     * @return:
     * @author: conceit
     * @Date: 2018/7/20 0020
     */
    @Test
    public void doTestExampleTwo() {

        Map<Employee.Gender, Employee> highestEarnerByGender = TestToMapThree.persons().stream().collect(
                Collectors.toMap(Employee::getGender,
                        Function.identity(),
                        (oldPerson, newPerson) ->
                                newPerson.getId() > oldPerson.getId() ? newPerson : oldPerson)
        );
        System.out.println(highestEarnerByGender);
    }


    /**
     * @Description: 第三个方法
     * 按照id分组，选取收入最高的人
     * @param:
     * @return:
     * @author: conceit
     * @Date: 2018/7/20 0020
     */
    @Test
    public void doTestExampleThree() {

        Map<Long, Employee> highestEarnerByGender = TestToMapThree.persons().stream().collect(
                //第一个形参就是oldPerson
                Collectors.toMap(Employee::getId,
                        Function.identity(),
                        (oldPerson, newPerson) ->
                                newPerson.getIncome() > oldPerson.getIncome() ? newPerson : oldPerson)
        );
        System.out.println(highestEarnerByGender);
    }


    public static List<Employee> persons() {
        Employee p1 = new Employee(1, "Jake", Employee.Gender.FEMALE, LocalDate.of(1971,
                Month.JANUARY, 1), 2343.0);
        Employee p2 = new Employee(1, "Jack", Employee.Gender.MALE, LocalDate.of(1972,
                Month.JULY, 21), 7100.0);
        Employee p3 = new Employee(3, "Jane", Employee.Gender.FEMALE, LocalDate.of(1972,
                Month.MAY, 29), 5455.0);
        Employee p4 = new Employee(4, "Jode", Employee.Gender.MALE, LocalDate.of(1976,
                Month.OCTOBER, 16), 1800.0);
        Employee p5 = new Employee(5, "Jeny", Employee.Gender.FEMALE, LocalDate.of(1972,
                Month.DECEMBER, 13), 1234.0);
        Employee p6 = new Employee(6, "Jason", Employee.Gender.MALE, LocalDate.of(1976,
                Month.JUNE, 9), 3211.0);

        List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

        return persons;
    }

    @Data
    public static class Employee {

        public enum Gender {
            MALE, FEMALE, Other
        }

        private long id;
        private String name;
        private Gender gender;
        private LocalDate dob;
        private double income;

        public Employee(long id, String name, Employee.Gender gender, LocalDate dob,
                        double income) {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.dob = dob;
            this.income = income;
        }

        public Gender getGender() {
            return gender;
        }

        public long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

    }
}
